Star (-) Watch (-)

Blog

Express Static Middleware and Gzip Express has great built in capabilities to serve static content. We can gzip the static content and cache it To add the static middleware app.use(express.static(__dirname + '/public'));

The static middleware handles serving up the content from the given directory. The static contents in the directory 'public' is served to the clients.

Cache-Control

To add caching of the content update the code with maxAge. app.use(express.static(__dirname + '/public', { maxAge: 86400000 })); Here 86400000 is equals to one day in milli seconds. ie 60 60 24 * 1000. This code will set the Cache-Control header to one day Check the header in Chrome debugger Cache-Control:max-age=86400000

Gzip: Use compress middleware to gzip app.use(express.compress()); Make sure that the app.use call for compress before any other middlewares such as logging.